﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace gui {
    /// <summary>
    /// Interaction logic for LoadingBox.xaml
    /// This box is presented during long running, non-interactive processes
    /// </summary>
    public partial class LoadingBox : Window {

        public LoadingBox(string status) {

            // Register Connection Listeners
            NetworkModelConnect.OnConnectedEvent += CloseLoadingBox;
            NetworkModelConnect.OnFinishedProcessingEvent += CloseLoadingBox;
            NetworkModelConnect.OnErrorEvent += CloseLoadingBox;
            PlayControl.OnFinishedProcessingEvent += CloseLoadingBox;
            Graph.OnFinishedProcessingEvent += CloseLoadingBox;
            Initialise.LoadingBoxStatusUpdate += UpdateStatus;


            InitializeComponent();
            StatusXAML.Content = status;

            this.Topmost = true;
        }

        public LoadingBox(string status, bool loadNow) {

            // Register Connection Listeners
            NetworkModelConnect.OnConnectedEvent += CloseLoadingBox;
            NetworkModelConnect.OnFinishedProcessingEvent += CloseLoadingBox;
            NetworkModelConnect.OnErrorEvent += CloseLoadingBox;
            PlayControl.OnFinishedProcessingEvent += CloseLoadingBox;
            Graph.OnFinishedProcessingEvent += CloseLoadingBox;
            Initialise.LoadingBoxStatusUpdate += UpdateStatus;

            InitializeComponent();
            StatusXAML.Content = status;
            if (loadNow) this.Show();
        }

        public void UpdateStatus(string status) {
            if (status == "Close") {
                CloseLoadingBox();
                return;
            }

            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(delegate() {
                    StatusXAML.Content = status;
            }));
        }

        private void CloseLoadingBox() {
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(delegate() {
                    this.Close();
            }));
        }
    }
}
